<?php
InitGP(array('action')); //初始化变量全局返回
include_once(INC_PATH."/goodsforecast.class.php");
include_once(INC_PATH."/delivery.class.php");

$d = new DeliveryClass();
$g = new GoodsForecastClass();
$uid = $_USERS['uid'];
$uname = $_USERS['uname'];
if($action == 'push'){
        InitGP(array('gid','address', 'remark', 'receipt', 'firm', 'box')); //初始化变量全局返回
        $goods = array();
        if($gid){
                $allWeight = 0;
                foreach($gid as $key=>$ggid){
                        $good = $g->getone($ggid);
                        if($good && ($good['uid'] == $uid)){
                                $goods[$good['gid']] = $good;
                                $allWeight = $good['weight'] + $allWeight;
                        }else{
                                showmsg('没有权利修改', $gourl);
                        }
                }
                if($allWeight > 30){
                        showmsg('重量超过30kg无法生成订单', -1);
                }
                //获取发货地址
                $r = new TableClass("address","aid");
                $wheresql = 'uid='.$uid;
                $addresses = $r->getdata("",$wheresql,'def DESC'); //获取团购数据
                if(!$addresses){
                        showmsg('你还没有收货地址，请先添加收货地址', '?name=myaddress');
                }
                //判断是否有发货信息
                if($address){
                        include_once(INC_PATH."/delivery.class.php");
                        //判断是否存在已发货
                        foreach ($addresses as $a){
                                if($address == $a['aid']){
                                        $address = $a;
                                }
                        }
                        if($address){
                                unset($address['def']);
                                unset($address['aid']);
                                $delivery = $address;
                                $delivery['addtime'] = time();
                                $delivery['remark'] = $remark;
                                $delivery['gids'] = implode(',', $gid);//TODO 重复性验证
                                $delivery['receipt'] = intval($receipt);
                                $delivery['firm'] = intval($firm);
                                $delivery['box'] = intval($box);
                                $d = new DeliveryClass();
                                $rs = $d->add($delivery);
                                if($rs){
                                        foreach($gid as $ggid){
                                                $g->editStatus($ggid, 4);
                                        }
                                        showmsg('提交发货成功', '?name=delivery');
                                }else{
                                        showmsg('提交发货失败', '?name=delivery');
                                }
                        }else{
                                showmsg('收货地址错误', -1);
                        }
                        exit;
                }
        }else{
                showmsg('必须选择出库', -1);
        }
        include template('member_delivery');//包含输出指定模板
}else{
        //当前订单
        $Table=new TableClass("goods_delivery","did");
        AjaxHead();//禁止页面缓存
        $total = $Table->getcount();
        $pagesize = 15;                                                                                           //一页显示信息数
        $page = isset($page) ? max(1, intval($page)) : 1;             //处理页码变量
        $offset = ($page-1) * $pagesize;        //偏移量
        $orderstr = 'did DESC';
        $wheresql = 'uid = '.$uid.' AND status < 8';
        $dataarray = $Table->getdata("$offset,$pagesize", $wheresql, $orderstr);
        foreach ($dataarray as $key=>$ex){
                $dataarray[$key]['status_info'] = $d->getStatusInfo($ex['status']);
        }
        include template('member_goods_delivery_list');
}
?>